<?php 

/**
 * @author Thanhansoft
 * @email thanhansoft@gmail.com
 * @website http://grouplaptrinh.com
 * @copyright 5/2/2011 7:11
 */
 
if (!defined('IN_SITE')) exit('Not allowed.');
	
class Application{
	public $uri;
	private $model;
	
	public function __construct($uri){
		$this->uri = $uri;
	}
	
	public function loadController($class){
		$file = "app/controller/".$this->uri['controller'].".php";
		if(!file_exists($file)){echo 'Module does not exist'; die();}
		require_once($file);
		$controller = new $class();
		if(method_exists($controller, $this->uri['method'])){
			$controller->{$this->uri['method']}($this->uri['var']);
		} else {
			$controller->index();
		}
	}
	
	protected function loadView($view, $vars=''){
		if(is_array($vars) && count($vars) > 0)
			extract($vars, EXTR_PREFIX_SAME, "wddx");
		require_once($view.'.php');
	}
	
	protected function loadModel($model){
	    $rows = return_array_multi("SELECT load_name, concat('model_', load_name) as model_name FROM `dos_sys_loads` WHERE dos_sys_models_model_id = '$model'");
        $rowsize = count($rows);
        if($rowsize > 0){
            for($i = 1; $i <= $rowsize; $i++){
                require_once('app/model/'.$rows[$i]['load_name'].'/model_'.$rows[$i]['load_name'].'.php');
                $this->$rows[$i]['model_name'] = new $rows[$i]['model_name'];
            }
        }else{
            echo 'Module does not exist in database';
            exit();
        }
	}
    
	public function loadFunction($module, $position) {
		$rows = return_array_multi("SELECT function_name, model_main FROM `dos_sys_functions` WHERE dos_sys_models_model_id = '$module' AND position = '$position' AND enable = 1 ORDER BY sort ASC LIMIT 0 , 30");
		$rowsize = count($rows);
		if($rowsize > 0){
			//call all function at $module
			for($i = 1; $i <= $rowsize; $i++){
				$this->{'model_'.$rows[$i]['model_main']}->$rows[$i]['function_name']();
			}
		}
	}
    
    protected function loadCss($fileName) {
		return '<link type="text/css" rel="stylesheet" href="'.TEMPLATE_SITE.'/css/'.$fileName.'.css" />';
	}
}

?>